using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Net.Mail;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

    public class Utility
    {
        public Utility()
        {
            
        }

        public static byte[] getEncryptedValue(string p_strPlainString)
        {
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            UTF8Encoding encoder = new UTF8Encoding();
            byte[] hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(p_strPlainString));
            return hashedBytes;
        }
        public static string cleanseText(string p_str)
        {
            p_str = p_str.Trim().Replace("'", "''");
            return p_str;
        }

        public static string GetGUID()
        {
            //string strFileNameWithoutExtension = FileName.Substring(0, FileName.LastIndexOf("."));
            return System.Guid.NewGuid().ToString();
        }
        
        public static string GetFileNameWithoutExtension(string FileName)
        {
            string strFileNameWithoutExtension = FileName.Substring(0, FileName.LastIndexOf("."));
            return strFileNameWithoutExtension;
        }
        
        public static bool IsValidDate(String date, String format)
        {
            try
            {
                System.Globalization.DateTimeFormatInfo dtfi = new
                       System.Globalization.DateTimeFormatInfo();
                dtfi.LongDatePattern = format;
                DateTime dt = DateTime.ParseExact(date, "D", dtfi);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public static int GetCurrentAge(string p_strDOB)
        {
            int iAge = 0;
            try
            {
                int iCurrentYear = DateTime.Today.Year;
                DateTime dtDOB = DateTime.ParseExact(p_strDOB, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
                iAge = iCurrentYear - dtDOB.Year;
            }
            catch (Exception exp)
            {

            }
            return iAge;
        }

        public static bool SendEmail(string p_from, string p_to, string p_subject, string p_body)
        {
            bool b_status = false;
            string sendto = "";
            string subject = "";
            string body = "";
            string from = "";
            string smtpclient = "";
            string smtppassword = "";

            try
            {
                from = p_from;
                sendto = p_to;
                subject = p_subject;
                body = p_body;

                smtpclient = ConfigurationSettings.AppSettings.Get("SMTPClient");
                smtppassword = ConfigurationSettings.AppSettings.Get("SMTPPassword");

                MailMessage message = new MailMessage(from, sendto, subject, body);
                SmtpClient emailClient = new SmtpClient(smtpclient);
                System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(from, smtppassword);
                emailClient.UseDefaultCredentials = false;
                emailClient.Credentials = SMTPUserInfo;
                emailClient.Send(message);
                b_status = true;
            }
            catch (Exception e)
            {
                b_status = false;
            }
            finally
            {

            }
            return b_status;
        }

        public static bool SendEmail(string p_to, string p_subject, string p_body)
        {
            bool b_status = false;
            string sendto = "";
            string subject = "";
            string body = "";
            string from = "";
            string smtpclient = "";
            string smtppassword = "";

            try
            {
                smtpclient = ConfigurationSettings.AppSettings.Get("SMTPClient");
                smtppassword = ConfigurationSettings.AppSettings.Get("SMTPPassword");
                from = ConfigurationManager.AppSettings.Get("MailFrom"); ;
                sendto = p_to;
                subject = p_subject;
                body = p_body;

                MailMessage message = new MailMessage(from, sendto, subject, body);
                SmtpClient emailClient = new SmtpClient(smtpclient);
                System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(from, smtppassword);
                emailClient.UseDefaultCredentials = false;
                emailClient.Credentials = SMTPUserInfo;
                emailClient.Send(message);
                b_status = true;
            }
            catch (Exception e)
            {
                b_status = false;
            }
            finally
            {

            }
            return b_status;
        }
        public static bool SendBulkEmail(string p_to, string p_subject, string p_body)
        {
            bool b_status = false;
            string sendto = "";
            string subject = "";
            string body = "";
            string from = "";
            string smtpclient = "";
            string smtppassword = "";

            try
            {
                smtpclient = ConfigurationSettings.AppSettings.Get("SMTPClient");
                smtppassword = ConfigurationSettings.AppSettings.Get("SMTPPassword");
                from = ConfigurationManager.AppSettings.Get("MailFrom"); ;
                sendto = p_to;
                subject = p_subject;
                body = p_body;

                MailMessage message = new MailMessage(from, sendto, subject, body);
                SmtpClient emailClient = new SmtpClient(smtpclient);
                System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(from, smtppassword);
                emailClient.UseDefaultCredentials = false;
                emailClient.Credentials = SMTPUserInfo;
                emailClient.Send(message);
                b_status = true;
            }
            catch (Exception e)
            {
                b_status = false;
            }
            finally
            {

            }
            return b_status;
        }

        /// Creates a resized bitmap from an existing image on disk.
        public static Bitmap CreateThumbnail(string p_strFilename, int p_iWidth, int p_iHeight)
        {
            System.Drawing.Bitmap bmpOut = null;
            try
            {
                Bitmap loBMP = new Bitmap(p_strFilename);
                ImageFormat loFormat = loBMP.RawFormat;
                decimal lnRatio;
                int lnNewWidth = 0;
                int lnNewHeight = 0;

                //*** If the image is smaller than a thumbnail just return it
                if (loBMP.Width < p_iWidth && loBMP.Height < p_iHeight)
                    return loBMP;

                if (loBMP.Width > loBMP.Height)
                {
                    lnRatio = (decimal)p_iWidth / loBMP.Width;
                    lnNewWidth = p_iWidth;
                    decimal lnTemp = loBMP.Height * lnRatio;
                    lnNewHeight = (int)lnTemp;
                }
                else
                {
                    lnRatio = (decimal)p_iHeight / loBMP.Height;
                    lnNewHeight = p_iHeight;
                    decimal lnTemp = loBMP.Width * lnRatio;
                    lnNewWidth = (int)lnTemp;
                }
                // System.Drawing.Image imgOut =
                //      loBMP.GetThumbnailImage(lnNewWidth,lnNewHeight,
                //                              null,IntPtr.Zero);


                // *** This code creates cleaner (though bigger) thumbnails and properly
                // *** and handles GIF files better by generating a white background for
                // *** transparent images (as opposed to black)
                bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
                Graphics g = Graphics.FromImage(bmpOut);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
                g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
                loBMP.Dispose();
            }
            catch
            {
                return null;
            }
            return bmpOut;
        }

        /// Creates a resized bitmap from an existing image on disk.
        public static System.Drawing.Image CreateThumbnail2(string p_strFilename, int p_iWidth, int p_iHeight)
        {
            System.Drawing.Image imgOut = null;
            try
            {
                Bitmap loBMP = new Bitmap(p_strFilename);
                ImageFormat loFormat = loBMP.RawFormat;
                decimal lnRatio;
                int lnNewWidth = 0;
                int lnNewHeight = 0;

                //*** If the image is smaller than a thumbnail just return it
                if (loBMP.Width < p_iWidth && loBMP.Height < p_iHeight)
                    return loBMP;

                if (loBMP.Width > loBMP.Height)
                {
                    lnRatio = (decimal)p_iWidth / loBMP.Width;
                    lnNewWidth = p_iWidth;
                    decimal lnTemp = loBMP.Height * lnRatio;
                    lnNewHeight = (int)lnTemp;
                }
                else
                {
                    lnRatio = (decimal)p_iHeight / loBMP.Height;
                    lnNewHeight = p_iHeight;
                    decimal lnTemp = loBMP.Width * lnRatio;
                    lnNewWidth = (int)lnTemp;
                }
                imgOut = loBMP.GetThumbnailImage(lnNewWidth, lnNewHeight,
                                                 null, IntPtr.Zero);


                // *** This code creates cleaner (though bigger) thumbnails and properly
                // *** and handles GIF files better by generating a white background for
                // *** transparent images (as opposed to black)
                //bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
                //Graphics g = Graphics.FromImage(bmpOut);
                //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                //g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
                //g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
                loBMP.Dispose();
            }
            catch
            {
                return null;
            }
            return imgOut;
        }

        public static void FormatEmptyGridRow(ref GridView p_gv, string p_strMsg)
        {
            int TotalColumns = p_gv.Rows[0].Cells.Count;
            p_gv.Rows[0].Cells.Clear();
            p_gv.Rows[0].Cells.Add(new TableCell());
            p_gv.Rows[0].Cells[0].ColumnSpan = TotalColumns;
            p_gv.Rows[0].Cells[0].Text = p_strMsg;
            p_gv.Rows[0].Cells[0].BackColor = System.Drawing.Color.White;
            p_gv.Rows[0].Cells[0].ForeColor = System.Drawing.Color.FromName("#A90A08"); //System.Drawing.Color.FromName("#A90A08");
            p_gv.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
            p_gv.Rows[0].Cells[0].Font.Bold = true;
            string[] arrFonts = { "Verdana", "Arial", "Helvetica", "sans-serif" };
            p_gv.Rows[0].Cells[0].Font.Names = arrFonts;
            p_gv.Rows[0].Cells[0].Font.Size = FontUnit.Parse("11px");
        }

    } //End Class

